﻿using Common;
using System;
using System.Diagnostics;

namespace CarPCBATest.Procedure
{
    internal class Jlink
    {
        public static bool WriteJflash(string jflashFilePath, string jflashExePath, string jLinkSn)
        {
            //string cmd = $"-openprj\"{jflashFilePath}\" -auto ";
            //string cmd = $"-usb{jLinkSn} -openprj\"{jflashFilePath}\" -auto";
            string cmd = $"-usb{jLinkSn} -openprj\"{jflashFilePath}\" -auto -exit";

            Process jflashProcess = new Process();
            jflashProcess.StartInfo.FileName = jflashExePath;
            jflashProcess.StartInfo.Arguments = cmd;
            jflashProcess.StartInfo.UseShellExecute = false;
            jflashProcess.StartInfo.RedirectStandardOutput = true;
            jflashProcess.StartInfo.RedirectStandardError = true;
            jflashProcess.StartInfo.CreateNoWindow = true;

            try
            {
                // 启动进程并等待完成
                jflashProcess.Start();
                jflashProcess.WaitForExit(5000);

                // 检查J-Flash的退出代码
                int exitCode = jflashProcess.ExitCode;
                if (exitCode == 0)
                {
                    Mylog.Debug("JFlash烧写成功");
                    return true;
                }
                else
                {
                    Mylog.Error($"JFlash烧写失败，退出代码: {exitCode}");
                    throw new Exception($"JFlash烧写失败，退出代码: {exitCode}");
                }
            }
            catch (Exception ex)
            {
                Mylog.Error($"发生错误: {ex.Message}");
                return false;
            }
            finally
            {
                // 关闭进程和释放资源
                jflashProcess.Close();
                jflashProcess.Dispose();
            }
        }

        public static bool WriteJflash(string jflashFilePath, string jflashExePath)
        {
            //string cmd = $"-openprj\"{jflashFilePath}\" -auto ";
            string cmd = $"-openprj\"{jflashFilePath}\" -auto -exit";

            Process jflashProcess = new Process();
            jflashProcess.StartInfo.FileName = jflashExePath;
            jflashProcess.StartInfo.Arguments = cmd;
            jflashProcess.StartInfo.UseShellExecute = false;
            jflashProcess.StartInfo.RedirectStandardOutput = true;
            jflashProcess.StartInfo.RedirectStandardError = true;
            jflashProcess.StartInfo.CreateNoWindow = true;

            try
            {
                // 启动进程并等待完成
                jflashProcess.Start();
                jflashProcess.WaitForExit();

                // 检查J-Flash的退出代码
                int exitCode = jflashProcess.ExitCode;
                if (exitCode == 0)
                {
                    Mylog.Debug("JFlash烧写成功");
                    return true;
                }
                else
                {
                    Mylog.Error($"JFlash烧写失败，退出代码: {exitCode}");
                    throw new Exception($"JFlash烧写失败，退出代码: {exitCode}");
                }
            }
            catch (Exception ex)
            {
                Mylog.Error($"发生错误: {ex.Message}");
                return false;
            }
            finally
            {
                // 关闭进程和释放资源
                jflashProcess.Close();
                jflashProcess.Dispose();
            }
        }
    }
}